﻿#region Disclaimer / License
// Copyright (C) 2011, Kenneth Bergeron, IAP Worldwide Services, Inc
// NOAA :: National Marine Fisheries Service
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
#endregion
using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;

namespace CatchProcessing
{
    /// <summary>
    /// Edit the Support database
    /// </summary>
    public partial class UtilityDialog : Form
    {
        /// <summary>
        /// Edit the Support database
        /// </summary>
        public UtilityDialog()
        {
            InitializeComponent();
            Views = new DataGridView[] { this.VesselsDataGridView, this.SamplesDataGridView, this.LengthsDataGridView, this.TagDataGridView };
            DataTable[] Bases = new DataTable[] { DataSets.Glob.Support.VesselCodes, DataSets.Glob.Support.MeasurementGeneralCodes, DataSets.Glob.Support.LengthSampleCodes, DataSets.Glob.Support.TagCodes };
            for(int i=0; i<Views.Length; i++)
            {
                Views[i].AutoGenerateColumns = false;
                Views[i].DataSource = Bases[i];
                Views[i].Columns[0].DataPropertyName = "isactive";
                Views[i].Columns[1].DataPropertyName = "CODE";
                Views[i].Columns[2].DataPropertyName = "DESCRIPTION";
                Views[i].Sort(Views[i].Columns[2], ListSortDirection.Ascending);
            }
        }
        /// <summary>
        /// Show the name of the selected database
        /// </summary>
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.toolStripLabel1.Text = this.tabControl1.TabPages[this.tabControl1.SelectedIndex].Text;
        }
        /// <summary>
        /// All the DGV's
        /// </summary>
        private DataGridView[] Views = null;
        /// <summary>
        /// Add pressed
        /// </summary>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            // Add to the selected DGV
            Views[tabControl1.SelectedIndex].Rows.Add();
        }
        /// <summary>
        /// Delete pressed
        /// </summary>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (Views[tabControl1.SelectedIndex].SelectedRows.Count == 0) return;
            if (MessageBox.Show("Delete selected items?", "DELETE", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No) return;
            for (int i = 0; i < Views[tabControl1.SelectedIndex].SelectedRows.Count; i++)
                Views[tabControl1.SelectedIndex].Rows.RemoveAt(Views[tabControl1.SelectedIndex].SelectedRows[i].Index);
        }
        /// <summary>
        /// OK pressed, save and exit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OKButton_Click(object sender, EventArgs e)
        {
            foreach(DataGridView G in Views)
                G.EndEdit();
            DataSets.Glob.Support.Save();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
        /// <summary>
        /// Cancel pressed, close
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CanToolStripButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }
        /// <summary>
        /// Clear the XML dataset
        /// </summary>
        private void initializeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("This will completly remove all data in the Catch database", "WARNING", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.Cancel) return;
            try
            {
                System.IO.File.Delete(DataSets.LocalDataSet.DefaultXMLFile);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                Close();
            }
            catch (Exception Ex)
            {
                MessageBox.Show("Error removing " + DataSets.LocalDataSet.DefaultXMLFile + ": " + Ex.Message);
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("This feature is ONLY for demo and testing, it will erase all the data.  Continue?",
                "WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != System.Windows.Forms.DialogResult.Yes)
                return;
            Program.UnPost();
        }
    }
}
